#ifdef USEJNI
#include "FFT_JNI.h"
using namespace yzrilyzr_dsp;
JNIEXPORT void JNICALL Java_yzrilyzr_dsp_FFT_fft(JNIEnv *, jobject, jlong value, jlong sinCosTable, jint M, jint complexLength){
	Complex ** complex=reinterpret_cast<Complex **>(value);
	double ** scTable=reinterpret_cast<double **>(sinCosTable);
	FFT::fft(complex, scTable, M, complexLength);
}
JNIEXPORT jlong JNICALL Java_yzrilyzr_dsp_FFT_generateComplex(JNIEnv *, jobject, jint fftLength){
	Complex ** obj=FFT::generateComplexArray(fftLength);
	return reinterpret_cast<jlong>(obj);
}
JNIEXPORT void JNICALL Java_yzrilyzr_dsp_FFT_deleteComplex(JNIEnv *, jobject, jlong ptr, jint complexLength){
	Complex ** obj=reinterpret_cast<Complex **>(ptr);
	FFT::deleteComplexArray(obj, complexLength);
}
JNIEXPORT jlong JNICALL Java_yzrilyzr_dsp_FFT_generateSinCosTable(JNIEnv *, jobject, jint fftLength){
	double ** obj=FFT::generateSinCosTable(fftLength);
	return reinterpret_cast<jlong>(obj);
}
JNIEXPORT void JNICALL Java_yzrilyzr_dsp_FFT_deleteSinCosTable(JNIEnv *, jobject, jlong ptr){
	double ** obj=reinterpret_cast<double **>(ptr);
	delete[] obj[0];
	delete[] obj[1];
	delete[] obj;
}
JNIEXPORT jint JNICALL Java_yzrilyzr_dsp_FFT_computeM(JNIEnv *, jobject, jint fftLength){
	return FFT::computeM(fftLength);
}
JNIEXPORT void JNICALL Java_yzrilyzr_dsp_FFT_inputComplex(JNIEnv * env, jobject, jlong complexPtr, jobject array, jint complexLength){
	Complex ** obj=reinterpret_cast<Complex **>(complexPtr);
	double * elements=static_cast<double *>(env->GetDirectBufferAddress(array));
	if(elements == nullptr){
		cerr << "Buffer nullptr" << endl;
		return;
	}
	FFT::inputDoubleArray(obj, elements, complexLength);
}
JNIEXPORT void JNICALL Java_yzrilyzr_dsp_FFT_inputComplexRingBuffer(JNIEnv * env, jobject, jlong complexPtr, jlong ringBufferPtr, jint complexLength){
	Complex ** obj=reinterpret_cast<Complex **>(complexPtr);
	RingBuffer * buf=reinterpret_cast<RingBuffer *>(ringBufferPtr);
	FFT::inputRingBuffer(obj, buf, complexLength);
}
JNIEXPORT void JNICALL Java_yzrilyzr_dsp_FFT_outputComplex(JNIEnv * env, jobject, jlong complexPtr, jobject array, jint complexLength){
	Complex ** obj=reinterpret_cast<Complex **>(complexPtr);
	double * elements=static_cast<double *>(env->GetDirectBufferAddress(array));
	if(elements == nullptr){
		cerr << "Buffer nullptr" << endl;
		return;
	}
	for(u_index i=0; i < complexLength; i++){
		Complex * cp=obj[i];
		double re=cp->re;
		double im=cp->im;
		elements[i]=sqrt(re * re + im * im);
	}
}
JNIEXPORT void JNICALL Java_yzrilyzr_dsp_FFT_multiple(JNIEnv *, jobject, jlong a, jlong b, jlong out, jint fftLength){
	Complex ** aa=reinterpret_cast<Complex **>(a);
	Complex ** bb=reinterpret_cast<Complex **>(b);
	Complex ** oo=reinterpret_cast<Complex **>(out);
	FFT::multiple(aa, bb, oo, fftLength);
}
#endif
